<?php

include("config.php");
if (!(isset($_SESSION["user"]) && $_SESSION["user"] != "") 
&&(!isset($_SESSION["auser"]) || $_SESSION["auser"] == "" || $_SESSION['agroup'] != "WEB_ADMIN") )
{
header('Location: index.php');
die();
}

$db_rdata = new DB($rdata_server_username,$rdata_server_pass,$rdata_server_db,$rdata_server,$rdata_driver);
$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);

$err = "";


if (isset($_REQUEST['month']) && is_numeric($_REQUEST['month'])){
	$month = $_REQUEST['month'];
} else {
	$month = date("m", strtotime("-0 months"));
}


if (isset($_REQUEST['year']) && is_numeric($_REQUEST['year'])){
	$year = $_REQUEST['year'];
} else {
	$year = date("Y");
}





include("header.php");
include("menu.php");


?>
<script type="text/javascript">
	$(document).ready(function(){	

		if (!$.browser.opera) {
    
			// select element styling
			$('select.select').each(function(){
				var title = $(this).attr('title');
				if( $('option:selected', this).val() != ''  ) title = $('option:selected',this).text();
				$(this)
					.css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
					.after('<span class="select">' + title + '</span>')
					.change(function(){
						val = $('option:selected',this).text();
						$(this).next().text(val);
						})
			});

		};
		
	});
	

	
</script>


<?php



?>

<table class="innercont" cellspacing="0" cellpadding="10">
<tr><td class="leftcol">
<?php
		if ($err != ""){
			echo "<p style='color:red;font-weight:bold;'>".$err."</p>";
		}

	$f = "<form id='newbut' action='";
	if (isset($adm_menu)) {
		$f .= "a";
	}
	$f .= "protocol.php' method=\"post\" >";

	$f .= "<table cellspacing='0' cellpadding='5' class='params'>";
	
	$f .= "<tr><th>Pasirinkite periodą:</th></tr>";
	
	
		$f .= "<tr><th>Metai:</th></tr>";
		$f .= "<tr><td style='text-align:left;'><input  class=\"inp\" type='text' name=\"year\" value=\"" . $year . "\"></td></tr>";
		
	$f .= "<tr><th>Mėnuo:</th></tr>";
	$f .= "<tr><td style='text-align:left;'>";
        $f .= '<div>';
        $f .= '<select name="month" class="select" >';
		$f .= "<option value=\"1\"";  if ($month=="1") {$f .= 'selected="selected"';}$f .= ">Sausis</option>";
		$f .= "<option value=\"2\"";  if ($month=="2") {$f .= 'selected="selected"';}$f .= ">Vasaris</option>";
		$f .= "<option value=\"3\"";  if ($month=="3") {$f .= 'selected="selected"';}$f .= ">Kovas</option>";
		$f .= "<option value=\"4\"";  if ($month=="4") {$f .= 'selected="selected"';}$f .= ">Balandis</option>";
		$f .= "<option value=\"5\"";  if ($month=="5") {$f .= 'selected="selected"';}$f .= ">Gegužė</option>";
		$f .= "<option value=\"6\"";  if ($month=="6") {$f .= 'selected="selected"';}$f .= ">Birželis</option>";
		$f .= "<option value=\"7\"";  if ($month=="7") {$f .= 'selected="selected"';}$f .= ">Liepa</option>";
		$f .= "<option value=\"8\"";  if ($month=="8") {$f .= 'selected="selected"';}$f .= ">Rugpjūtis</option>";
		$f .= "<option value=\"9\"";  if ($month=="9") {$f .= 'selected="selected"';}$f .= ">Rugsėjis</option>";
		$f .= "<option value=\"10\"";  if ($month=="10") {$f .= 'selected="selected"';}$f .= ">Spalis</option>";
		$f .= "<option value=\"11\"";  if ($month=="11") {$f .= 'selected="selected"';}$f .= ">Lapkritis</option>";
		$f .= "<option value=\"12\"";  if ($month=="12") {$f .= 'selected="selected"';}$f .= ">Gruodis</option>";

		$f .= '</select>';
        $f .= '</div>		';
	$f .= "</td></tr>";
	$f .= "<tr><td ><input class='but bgfilter' type = \"submit\" value = \"Rodyti\"></td></tr>";
	$f .= "</table></form>";
	if (isset($adm_menu)) {
		$f .= '<table cellspacing="0" cellpadding="5" class="params"><tbody><tr><td style="text-align:center;"><a id="newButton" href="protocol_items.php"  class="card3">Protokolo prekės</a></td></tr></tbody></table><br>';
} else {
	$i_query = str_replace("<kodas>", $_SESSION['user'], $i_query);
	$minfo = $db_www->returnInfo($i_query);	
	$test_q = "SELECT N08_c_protokolas,N08_c_protokolas_n FROM n08_klij_c WHERE N08_c_kodas_ks = '".$minfo["N08_kodas_ks"]."'";
	$imm = $db_rdata->returnInfo($test_q);		
	if (!empty($imm)) {
		$nuol_dydis = trim($imm["N08_c_protokolas"]);
		$nuol_dydis2 = trim($imm["N08_c_protokolas_n"]);
	}else {
		$nuol_dydis = "";
		$nuol_dydis2 = "";
	}	
	$f .= '<table cellspacing="0" cellpadding="5" style="width:160px;">';
	$f .= '<tr><td>Protokolo antkainis</td><td valign=top><input style="width:90px;background-color:#eeeeee;" type="text" class="inp" readonly value="'.htmlspecialchars(sprintf("%01.3f",$nuol_dydis)).'"></td></tr>';
	if ($nuol_dydis2 != '' && $nuol_dydis2 != '0' && $nuol_dydis2 != null){
		$f .= '<tr><td>Protokolo antkainis partnerių deg.</td><td valign=top><input style="width:90px;background-color:#eeeeee;" type="text" class="inp" readonly value="'.htmlspecialchars(sprintf("%01.3f",$nuol_dydis2)).'"></td></tr>';		
	}
	$f .= '</table>';
}
echo $f;




?>
</td><td class="rightcol" rowspan="2">
<?php
    $sql = 'select * from x01_prot_n';
    $prekes = $db_rdata->returnList($sql);
    $pr = array();
	foreach ($prekes as $preke) {
	   $id = $preke['X01_kodas_ps']; 
	   $pr[$id] = $preke['X01_pav'];
	}
	if (isset($adm_menu)) {
		echo '<table cellspacing="0" cellpadding="3"><tr><td style="text-align:right;"><a class="flink" href="edit_protocol.php?year='.$year .'&month='.$month.'">Koreguoti lentelę</a></td></tr><tr><td>';
	}
	echo '<table cellspacing="0" cellpadding="3" class="innercont3">';
	echo '<tr><th>Data</th>';
	foreach ($pr as $x=>$xval) {
	   echo '<th>'.htmlspecialchars( $xval).'</th>';
	}
	echo '</tr>';
	$sql = 'select CONVERT(VARCHAR(10),X02_data, 102) as X02_data from x02_prot where year(X02_data) = '.$year.' and month(X02_data)= '.$month.' group by X02_data order by X02_data';
    $datos = $db_rdata->returnList($sql);
	
	foreach ($datos as $dt) {
	  $sql = "select * from x02_prot where X02_data='".$dt['X02_data']."'";
	  $eilute = $db_rdata->returnList($sql);
		$eil = array();
		foreach ($eilute as $preke) {
		   $id = $preke['X02_kodas_ps']; 
		   $eil[$id] = sprintf("%01.3f",$preke['X02_kaina']);
		}	
       	$tmp = '';
		 foreach ($pr as $x=>$xval) {
				if(isset($eil[$x])) {
				   $vv = $eil[$x];
				} else {
				  $vv ='';
				}
			   $tmp .= '<td style="text-align:right;">'.htmlspecialchars($vv ).'</td>';
			}     		
	  echo '<tr><td>'.$dt['X02_data'].'</td>'.$tmp.'</tr>';
	}
	echo '</table>';
	if (isset($adm_menu)) {
		echo '</td></tr></table>';
	}
?>
</td></tr>
<tr><td >

</td></tr>
</table>
<?php
include("footer.php");
?>